package com.wancaijinfu.component;


import com.wancaijinfu.constant.Constant;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

public class MSharePreferences {
	private final String TAG = "MSharePreferences";
	SharedPreferences spf;
	Editor editor;

	public MSharePreferences(Context context, String tag) {
		if (spf == null) {
			if (tag == null || tag.equals("")) {
				tag = Constant.APP;
			}
			spf = context.getSharedPreferences(tag, Context.MODE_PRIVATE);
			editor = spf.edit();
		}
	}

	public void save(String key, String value) {
		if (editor == null) {
			Log.e(TAG, "SharedPreferences为空");
			return;
		}
		editor.putString(key, value);
		editor.commit();
	}
	
	public void save(String key, long value) {
		if (editor == null) {
			Log.e(TAG, "SharedPreferences为空");
			return;
		}
		editor.putLong(key, value);
		editor.commit();
	}

	public void save(String key, int value) {
		if (editor == null) {
			Log.e(TAG, "SharedPreferences为空");
			return;
		}
		editor.putInt(key, value);
		editor.commit();
	}
	
	public void save(String key, boolean value) {
		if (editor == null) {
			Log.e(TAG, "SharedPreferences为空");
			return;
		}
		editor.putBoolean(key, value);
		editor.commit();
	}

	public Object get(String key, Class<?> cls) {
		if (editor != null) {
			if (cls == String.class) {
				return spf.getString(key, "");
			} else if (cls == Integer.class) {
				return spf.getInt(key, 0);
			} else if (cls == Long.class) {
				return spf.getLong(key, 0);
			} else if (cls == Boolean.class) {
				return spf.getBoolean(key, false);
			}
		}
		return null;
	}
	
	public void remove(String key) {
		if (editor != null) {
			editor.remove(key);
			editor.commit();
		}
	}

	public void clear() {
		if (editor != null) {
			editor.clear();
			editor.commit();
		}
	}
}
